const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function savePicToAlbum(tempFilePath) {
  let that = this;
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success(res) {
                wx.showToast({
                  title: '保存成功'
                });
              },
              fail(res) {
                console.log(res);
              }
            })
          },
          fail() {
            // 用户拒绝授权,打开设置页面
            wx.openSetting({
              success: function (data) {
                console.log("openSetting: success");
              },
              fail: function (data) {
                console.log("openSetting: fail");
              }
            });
          }
        })
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
            });
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    },
    fail(res) {
      console.log(res);
    }
  })
}

function image() {
  var imageSize = {};
  var originalScale = 0.2;//图片高宽比  
  //获取屏幕宽高  
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;//屏幕高宽比  
      console.log('windowWidth: ' + windowWidth)
      console.log('windowHeight: ' + windowHeight)
      if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
        //图片缩放后的宽为屏幕宽  
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * 400) / 320;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
        imageSize.imageTop = (windowHeight - imageSize.imageWidth)/2;
      } else {//图片高宽比大于屏幕高宽比  
        //图片缩放后的高为屏幕高  
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * 320) / 400;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
      }

    }
  })
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  console.log("图片的高度"+imageSize.imageTop)
  return imageSize;
}


function image2(width,height) {
  var imageSize = {};
  var originalScale = 0.2;//图片高宽比  
  //获取屏幕宽高  
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;//屏幕高宽比  
      
      if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
        //图片缩放后的宽为屏幕宽  
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * 400) / 320;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
        imageSize.imageTop = (windowHeight - imageSize.imageWidth) / 2;
      } else {//图片高宽比大于屏幕高宽比  
        //图片缩放后的高为屏幕高  
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * 320) / 400;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
      }

    }
  })
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  console.log("图片的高度" + imageSize.imageTop)
  return imageSize;
}

//加载中函数
function loading(){
  wx.showLoading({
    title: '玩命加载中...',
  })
  setTimeout(function(){
    wx.hideLoading();
  },300)
}

function countdown(that,hours,minutes,seconds) {
 var times = hours * 3600 + minutes * 60 + seconds * 1;
  var hour=0,minute=0,second=0;
  
  var timer = setInterval(function () {
    if (times > 0) {
      times--;
      hour = Math.floor(times / (60 * 60));
      minute = Math.floor(times / 60) - (hour * 60);
      second = Math.floor(times) - (hour * 60 * 60) - (minute * 60);
      if (hour <= 9) hour = '0' + hour;
      if (minute <= 9) minute = '0' + minute;
      if (second <= 9) second = '0' + second;
      that.setData({
        hours: hour,
        minutes: minute,
        seconds: second

      });
    }else{
    
    
        clearInterval(timer);
        wx.showModal({
          title: '很遗憾，该商品砍价失败，继续努力！',
          content: '',
          cancelColor: "#666",
          cancelText: "我知道了",
          confirmColor: "#ff1313",
          confirmText: "重砍一个",
          fail: function () {

          },
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })

            }
            if (res.cancel) {
              wx.redirectTo({
                url: '/pages/kanjian/kanjian?key=' + 2,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }

          }
        })
        return;
    
  
    }
    
  }, 1000)

 

   
  }
 

  


var hostUrl = "https://www.jikecity.xin/small/";

module.exports = {
  formatTime: formatTime,
  savePicToAlbum: savePicToAlbum,
  image: image,

  loading:loading,//页面提示正在加载中
  countdown: countdown,//页面倒计时
  
  jktc_logo:"https://www.jikecity.xin/pic/link.png"  ,
  erweima:"https://www.jikecity.xin/public/code.jpg",
  imgUrl:"/pages/images/",
  baseImgUrl:"https://www.jikecity.xin/public/images/",//服务器图片地址，公共部分
  getNewsDetail: hostUrl +"shouxq/index",
  getTuijianNews: hostUrl + "shouxq/tuijian",

  shoueyNav:hostUrl+"index/index",//获取首页导航栏
  down_load: hostUrl +"zuopin/down_load",//首页下拉加载
  up_load: hostUrl +"zuopin/up_load",//首页上拉加载

  zuopin_info: hostUrl +"zuopin/zuopin_info",//新闻名字初始数据
  guanzhu:hostUrl+"zuopin/guanzhu",//新闻明细加关注
  quguan:hostUrl+"zuopin/guanzhu_del",//新闻明细取消关注
  shoucang: hostUrl + "zuopin/shoucang",//新闻明细加收藏
  qu_shoucang: hostUrl + "zuopin/shoucang_del",//新闻明细取消收藏
  
  tuijian_newslist: hostUrl +"index/get_zuopin",//首页推荐下的新闻列表
  video_info: hostUrl +"zuopin/video_info",//视频新闻明细
  tuijian_audio: hostUrl +"shouyevideo/tuijian",//视频新闻推荐

  pindao_my: hostUrl + "pindao/index",//我的频道
  pindao_qita: hostUrl +"shouyeclass/jikeclass" ,//其他频道
  add_pindao: hostUrl +"pindao/addclass",//添加频道//
  del_pindao: hostUrl + "pindao/delclass",//删除频道
}
